import javax.swing.text.html.ObjectView;
import java.util.ArrayList;
import java.util.Iterator;

public class ListDemo {
    public static void t1(){
        //创建Arraylist
        //有序的 ， 可以重复的   可以放空元素
        //当Arraylist初始化的时候，自动创建了一个空的object的数组
        //当Arraylist创建的时候，如果没有指定泛型，那么默认就是Object类型
        ArrayList list = new ArrayList();

        //添加元素
        list.add("哈哈");
        list.add(123);  //123 -> 自动装箱 -> Integer
        list.add('A');  //Character
        list.add(null);
        list.add(true);

        //在指定位置添加元素
        list.add(2,"嘿嘿");

        //打印
        System.out.println("打印后的结果 ：" + list);

        //返回集合的长度.size
        System.out.println("集合的长度" + list.size());

        //获取集合元素
        int a = (int)list.get(1);
        System.out.println(a);

        //查找索引位置,返回第一次出现的索引
        int b = list.indexOf(123);
        System.out.println(b);

        //移除元素 返回boolean是否删除
        Object c = list.remove(Integer.valueOf(123));
        System.out.println(c);

        //判断是否包含某个元素
        boolean contains = list.contains(null);
        System.out.println(contains);   //返回boolean类型

        //修改元素
        list.set(0,"呜呜");
        System.out.println(list);

        //判断集合是否有元素
        boolean empty = list.isEmpty();
        System.out.println(empty);

        //清除所有元素
        list.clear();
        System.out.println(list);
    }

    //按照价钱对对对象进行排序
    public static void t2(){
        //集合存对象
        ArrayList list = new ArrayList();
        list.add(new Book(111,"五年单身，三年当狗","罗贼",50.5));
        list.add(new Book(112,"三年单身，两年当狗","罗畜",80.5));
        list.add(new Book(113,"两年单身，一年当狗","罗贱",70.5));
        list.add(new Book(114,"十年单身，十年当狗","罗贱",30.5));
//        Book book = (Book)list.get(1);
//        System.out.println(book.getBookName());
//        Object a = null;
//        for (int i = 0; i < list.size() -1; i++) {
//            for (int j = i+1; j < list.size(); j++){
//                Book book1 = (Book)list.get(i);
//                Book book2 = (Book)list.get(j);
//                if (book1.getPrice() > book2.getPrice()){
//                    a = book1;
//                    list.set(i,book2);
//                    list.set(j,a);
//                }
//            }
//        }

        for (int i = 0; i < list.size()-1; i++) {
            for (int j = 0; j < list.size()-1-i; j++) {
                Book boo1 = (Book) list.get(j);
                Book boo2 = (Book) list.get(j+1);
                if (boo1.getPrice() > boo2.getPrice()){
                    list.set(j,boo2);
                    list.set(j+1,boo1);
                }
            }
        }
        for (Object o : list) {
            System.out.println(o);
        }
    }
    public static void t3(){
        //迭代器
        ArrayList list = new ArrayList();
        list.add("哈哈");
        list.add(123);
        list.add("哈哈哈");
        list.add("哈哈哈哈");
        Iterator iterator = list.iterator();
//        if (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
    public static void t4(){
        /*
        //创建新数组的时候就创建一个默认容量空元素数据 elementData是空数组
        public ArrayList() {
            this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
        }

        //添加元素
        public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!! //判断内部容量
        elementData[size++] = e;
        return true;

        //判断
        private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }

        ensureExplicitCapacity(minCapacity);
    }
    }
         */
    }
    public static void t5(){
        /**
         * 代码解读：
         * 1. //添加方法
         *    public boolean add(E e) {
         *         //判断是否需要内部扩容(下一个元素的个数)
         *         ensureCapacityInternal(size + 1);  // Increments modCount!!
         *         //给数组元素赋值
         *         elementData[size++] = e;
         *         //返回
         *         return true;
         *     }
         * 2. //计算容量(数组,下一个元素的个数)
         *    private static int calculateCapacity(Object[] elementData, int minCapacity) {
         *         //判断数组是否是空数组
         *         if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
         *             //返回DEFAULT_CAPACITY和minCapacity的最大值
         *             //返回10
         *             return Math.max(DEFAULT_CAPACITY, minCapacity);
         *         }
         *         //返回下一个元素的个数
         *         return minCapacity;
         *     }
         *
         * 3. 明确的是否要扩容（下一个元素的个数）
         *  private void ensureExplicitCapacity(int minCapacity) {
         *         //修改次数+1
         *         modCount++;
         *
         *         // overflow-conscious code
         *          //判断是否需要扩容
         *          //如果下一个元素的个数大于了数组的长度，那么必须扩容，否则就不扩容
         *         if (minCapacity - elementData.length > 0)
         *             //扩容
         *             grow(minCapacity);
         *     }
         *
         * 4. 扩容的方法
         *    private void grow(int minCapacity) {
         *         // overflow-conscious code
         *         //得到原来数组的长度
         *         int oldCapacity = elementData.length;
         *         //得到新的数组的容量 = 源数组的长度 + 源数组的长度/2
         *         int newCapacity = oldCapacity + (oldCapacity >> 1);
         *         //如果新的数组的容量比下一个元素的个数小
         *         if (newCapacity - minCapacity < 0)
         *             //赋值
         *             newCapacity = minCapacity;
         *         //判断新的数组的容量如果超过了极限值的情况
         *         if (newCapacity - MAX_ARRAY_SIZE > 0)
         *             newCapacity = hugeCapacity(minCapacity);
         *         // minCapacity is usually close to size, so this is a win:
         *         //数组赋值的方法
         *         elementData = Arrays.copyOf(elementData, newCapacity);
         *     }
         */

    }

    public static void t6(){
        ArrayList list = new ArrayList();
        list.add(new Book(111,"五年单身，三年当狗","罗贼",50.5));
        list.add(new Book(112,"三年单身，两年当狗","罗畜",80.5));
        list.add(new Book(113,"两年单身，一年当狗","罗贱",70.5));
        Book book = new Book(111,"五年单身，三年当狗","贼",5.5);
        System.out.println(list.contains(book));
    }
    public static void main(String[] args) {
        t2();
    }
}
